package com.fance.highlight_spring4.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author fance
 * @date 18-5-6 上午10:29
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{

    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setBeanName(String s) {
        beanName = s;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
       loader = resourceLoader;
    }

    public void outputResult() {
        System.out.println("Bean的名称为: " + beanName);


        Resource resource =
                loader.getResource("classpath:test.txt");
        try{

            System.out.println("ResourceLoader加载文件内容为: " + IOUtils.toString(resource.getInputStream()));

        }catch(IOException e){
            e.printStackTrace();
        }
    }

}
